#include "widget.h"
#include "ui_widget.h"

#include <QLCDNumber>
#include <QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 设置一个计时器
    setCountDownTimer();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::setCountDownTimer()
{
    QLCDNumber* lcdNumber = new QLCDNumber(this);
    lcdNumber->move(200,200);
    lcdNumber->setObjectName("LCDNumber");
    lcdNumber->setFixedSize(200,100);

    // 设置显示风格
//    lcdNumber->setSegmentStyle(QLCDNumber::Flat);
//    lcdNumber->setSegmentStyle(QLCDNumber::Outline);
    lcdNumber->setSegmentStyle(QLCDNumber::Filled);

    timer = new QTimer(this);
    timer->setObjectName("Timer");

    lcdNumber->display(10);
    connect(timer, &QTimer::timeout, this, &Widget::updatetime);

    // 每隔1000毫秒触发一次
    timer->start(1000);
}


void Widget::updatetime()
{
    QLCDNumber* lcd = findChild<QLCDNumber*>("LCDNumber");
    QTimer* timer = findChild<QTimer*>("Timer");
    if(lcd != nullptr && timer != nullptr)
    {
        int value = lcd->intValue();
        if(value <= 0)
        {
            timer->stop();
            return;
        }

        lcd->display(value-1);
    }

}
